package com.fancy.gateway.controller;

import com.fancy.common.dto.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 网关监控控制器
 *
 * @author fancy
 */
@Slf4j
@RestController
@RequestMapping("/gateway")
public class GatewayController {

    @Autowired
    private RouteLocator routeLocator;

    /**
     * 网关健康检查
     */
    @GetMapping("/health")
    public Result<Map<String, Object>> health() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", System.currentTimeMillis());
        health.put("service", "gateway");
        health.put("version", "1.0.0");
        return Result.success(health);
    }

    /**
     * 获取路由信息
     */
    @GetMapping("/routes")
    public Result<Object> getRoutes() {
        try {
            return Result.success(routeLocator.getRoutes().collectList().block());
        } catch (Exception e) {
            log.error("获取路由信息失败", e);
            return Result.fail("获取路由信息失败");
        }
    }

    /**
     * 网关状态信息
     */
    @GetMapping("/info")
    public Result<Map<String, Object>> info() {
        Map<String, Object> info = new HashMap<>();
        info.put("name", "I Made A Sound Gateway");
        info.put("description", "在线音乐平台网关服务");
        info.put("version", "1.0.0");
        info.put("author", "fancy");

        Map<String, Object> features = new HashMap<>();
        features.put("authentication", "JWT Token认证");
        features.put("authorization", "VIP和管理员权限控制");
        features.put("rateLimit", "多维度限流保护");
        features.put("loadBalance", "服务负载均衡");
        features.put("fallback", "服务降级处理");
        features.put("cors", "跨域请求支持");

        info.put("features", features);

        return Result.success(info);
    }
}
